package com.example.myapplication.found.model;

import androidx.collection.ArraySet;

import com.example.myapplication.found.bean.FoundBean;
import com.example.myapplication.found.bean.StockBean;
import com.example.myapplication.found.bean.StockKlineBean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * @author: rongfeng
 * @date: 2022/7/28
 * @description
 */
public class DaBan extends BaseData {

    public static int shouPanCount = 0;
    DaBanCallback simpleCallback;
    public FoundBean foundBean;
    final long maxShizhi = 20000000000L;

    final int reDay = 180;
    public static String endDate;
    static List<String> codeList = new ArrayList<>();

    public DaBan(StockBean stockBean, StockBean weekStockBean) {
        super(stockBean, weekStockBean);
    }


    @Override
    public void start() {
        findStock();
//        deal1();
    }

    //首版分析
    private void deal1() {
        for (StockKlineBean stockKlineBean : stockBean.klineList) {
            if (stockKlineBean.chg > 9.7) {
                System.out.println(fundName);
            }
        }
    }


    private void deal2() {
        try {
            long shizhi = Long.parseLong(foundBean.f20);
            int index = 0;
            for (StockKlineBean stockKlineBean : stockBean.klineList) {
                if (stockKlineBean.shouPan < 10 && shizhi < maxShizhi) {
                    index++;
                    loop(index);
                }
            }
        } catch (Exception e) {

        }
    }


    private void loop(int index) {
        if (stockBean.klineList.size() > index) {
            StockKlineBean currentStockKlineBean = stockBean.klineList.get(index);
            float shouPan = currentStockKlineBean.shouPan;
            int dayCount = 0;
            for (int i = index - reDay; i > 0; i++) {
                StockKlineBean stockKlineBean = stockBean.klineList.get(i);
                if (shouPan >= stockKlineBean.shouPan) {
                    dayCount++;
                    if (dayCount >= reDay) {
                        System.out.println(fundName + " " + currentStockKlineBean.date + " 突破 " + reDay);
                        break;
                    }
                } else {
                    break;
                }
            }

            if (dayCount >= reDay) {
                int avgChg = 0;
                int count = 0;
                for (int i = index; i < 5 && stockBean.klineList.size() > i; i++) {
                    StockKlineBean stockKlineBean = stockBean.klineList.get(i);
                    avgChg += stockKlineBean.chg;
                    count++;
                }
                System.out.println("后5天平均 " + (avgChg / count));
            }
        }
    }

    private void findStock() {
        Collections.reverse(stockBean.klineList);
        StockKlineBean lastStockKlineBean = stockBean.klineList.get(0);
        float shouPan = lastStockKlineBean.shouPan;
        endDate = lastStockKlineBean.date;
        try {
            long shizhi = Long.parseLong(foundBean.f20);
            if (shouPan < 10 && shizhi < maxShizhi) {
                int dayCount = 0;
                for (StockKlineBean stockKlineBean : stockBean.klineList) {
                    if (shouPan >= stockKlineBean.shouPan) {
                        dayCount++;
                        if (dayCount >= reDay) {
                            if (!codeList.contains(fundCode)) {
                                codeList.add(fundCode);
                                shouPanCount++;
                                if (fundName.length() < 4) {
                                    fundName = fundName + "     ";
                                } else if (fundName.startsWith("ST")) {
                                    fundName = fundName + "  ";
                                }

                                System.out.println("    "+fundCode + " " + fundName + " 股价 " + shouPan + " 市值 " + (shizhi / 100000000) + " 亿");
                                break;
                            }
                        }
                    } else {
                        break;
                    }
                }
            }
        } catch (Exception e) {

        }
        if (simpleCallback != null) {
            simpleCallback.daBanCallback();
        }
    }

    public static void analysis() {
        System.out.println("    市值小于200亿，股价低于10元，今日涨停板");
        System.out.println("    收盘日期: " + endDate);

    }


    public void setSimpleCallback(DaBanCallback simpleCallback) {
        this.simpleCallback = simpleCallback;
    }

    public interface DaBanCallback {
        void daBanCallback();
    }
}
